package com.bobo.controller.front.message;

import com.bobo.annotations.RequestMapping;
import com.bobo.annotations.RequestMethod;
import com.bobo.controller.base.BaseServlet;
import com.bobo.dao.topic.ITopicDao;
import com.bobo.model.Message;
import com.bobo.model.User;
import com.bobo.service.message.IMessageService;
import com.bobo.service.message.MessageServiceImpl;
import com.bobo.service.topic.ITopicService;
import com.bobo.service.topic.TopicServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by bobo on 16-10-30.
 */
@WebServlet(name = "message",urlPatterns = {"/message/*"})
public class MessageController extends BaseServlet {

    IMessageService messageService = new MessageServiceImpl();

    ITopicService topicService = new TopicServiceImpl();

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(HttpServletRequest request, HttpServletResponse response) {
        User user = (User) request.getSession().getAttribute("user");
        int id = Integer.parseInt(request.getParameter("id"));
        String content = request.getParameter("content");
        Message message = new Message();
        message.setUser(user);

        message.setTopic(topicService.getById(id));
        message.setContent(content);
        messageService.create(message);
        return "redirect:/topic/get?id="+id;
    }
}
